//
//  XiaoKaiAttackViewController.m
//  Project_A
//
//  Created by 凯 on 15/7/14.
//  Copyright (c) 2015年 zhoukaidong. All rights reserved.
//

#import "XiaoKaiAttackViewController.h"
#import "AFNetWorkHandle.h"
#import "XiaoKaiAttackModel.h"
#import "BaseHeader.h"
#import "XiaoKaiAttackTableViewCell.h"
#import "SecondViewController.h"
#import "XiaoKaiRaidersViewController.h"
#import "MJRefresh.h"

@interface XiaoKaiAttackViewController ()
<
UITableViewDataSource,
UITableViewDelegate
>

@property (nonatomic , retain)NSMutableArray *arr;

@property (nonatomic , retain)UITableView *tableView;

@property (nonatomic , retain)UIButton *backbutton;

@property (nonatomic, assign) NSInteger footer;

@end

@implementation XiaoKaiAttackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.arr = [NSMutableArray array];
    self.footer = 20;
    if (self.tableView != nil) {
        
    }
    [self getData];

}
-(void)getData
{
    NSLog(@"%@",self.myid);
    if (self.Push == 0) {
        self.webstring =  [NSString stringWithFormat:@"http://api.liwushuo.com/v1/collections/%@/posts?gender=1&generation=2&limit=20&offset=0" , self.myid ];
        [AFNetWorkHandle GETwithAFNByURL:self.webstring completion:^(id result) {
            NSMutableArray *temparr = [[result objectForKey:@"data"]objectForKey:@"posts"];
            
            for (NSDictionary *dic in temparr) {
                XiaoKaiAttackModel *model = [[XiaoKaiAttackModel alloc]initWithDic:dic];
                [self.arr addObject:model];
                NSLog(@"model %@",model.cover_image_url);
                NSLog(@"%@",model.title);
            }
            
            [self creatTableView];
            [self creatNav];
        }];
        [self.tableView reloadData];
        [self.tableView.footer endRefreshing];
    } else {
    self.webstring =  [NSString stringWithFormat:@"http://api.liwushuo.com/v1/channels/%@/items?limit=20&offset=0" , self.myid ];
        [AFNetWorkHandle GETwithAFNByURL:self.webstring completion:^(id result) {
            NSMutableArray *temparr = [[result objectForKey:@"data"]objectForKey:@"items"];
            
            for (NSDictionary *dic in temparr) {
                XiaoKaiAttackModel *model = [[XiaoKaiAttackModel alloc]initWithDic:dic];
                [self.arr addObject:model];
                NSLog(@"model %@",model.cover_image_url);
                NSLog(@"%@",model.title);
            }
            
            [self creatTableView];
            [self creatNav];
        }];
        [self.tableView reloadData];
        [self.tableView.footer endRefreshing];
    }
}

-(void)getDataWithTableViewasd
{
    NSLog(@"%@",self.myid);
    if (self.Push == 0) {
        self.webstring =  [NSString stringWithFormat:@"http://api.liwushuo.com/v1/collections/%@/posts?gender=1&generation=2&limit=20&offset=%ld" , self.myid , self.footer];
        [AFNetWorkHandle GETwithAFNByURL:self.webstring completion:^(id result) {
            NSMutableArray *temparr = [[result objectForKey:@"data"]objectForKey:@"posts"];
            
            for (NSDictionary *dic in temparr) {
                XiaoKaiAttackModel *model = [[XiaoKaiAttackModel alloc]initWithDic:dic];
                [self.arr addObject:model];
                NSLog(@"model %@",model.cover_image_url);
                NSLog(@"%@",model.title);
            }
        }];
        self.footer += 20;
        [self.tableView reloadData];
        [self.tableView.footer endRefreshing];
    } else {
        self.webstring =  [NSString stringWithFormat:@"http://api.liwushuo.com/v1/channels/%@/items?limit=20&offset=%ld" , self.myid , self.footer];
        [AFNetWorkHandle GETwithAFNByURL:self.webstring completion:^(id result) {
            NSMutableArray *temparr = [[result objectForKey:@"data"]objectForKey:@"items"];
            
            for (NSDictionary *dic in temparr) {
                XiaoKaiAttackModel *model = [[XiaoKaiAttackModel alloc]initWithDic:dic];
                [self.arr addObject:model];
                NSLog(@"model %@",model.cover_image_url);
                NSLog(@"%@",model.title);
            }
        }];
        self.footer += 20;
        [self.tableView reloadData];
        [self.tableView.footer endRefreshing];
    }
}

-(void)creatTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH , SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
 //   [self.tableView release];
    if (self.arr != nil) {
        self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getDataWithTableViewasd)];
    }
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getData)];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return SCREEN_HEIGHT / 4 + 5;
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"attack";
    XiaoKaiAttackTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[XiaoKaiAttackTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    XiaoKaiAttackModel *model = self.arr[indexPath.row];
    cell.Model = model;
    return cell;
}

-(void)creatNav
{
    UIView *naview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20 )];
    naview.backgroundColor = NAVCOLOR;
    [self.view addSubview:naview];
    UILabel *Navlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 44)];
    Navlabel.backgroundColor = NAVCOLOR;
    Navlabel.text = @"精品攻略";
    Navlabel.font = [UIFont systemFontOfSize:22];
    Navlabel.textColor = [UIColor whiteColor];
    Navlabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:Navlabel];
    [self.view bringSubviewToFront:Navlabel];
    [self getbutton];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    SecondViewController *sec = [[SecondViewController alloc]init];
//    XiaoKaiAttackModel *model = [self.arr objectAtIndex:indexPath.row];
//    sec.attackwebstring = model.content_url;
//    sec.y = @"2";
//    [self.navigationController pushViewController:sec animated:YES];
    XiaoKaiRaidersViewController *raid = [[XiaoKaiRaidersViewController alloc]init];
    XiaoKaiAttackModel *model = [self.arr objectAtIndex:indexPath.row];
    raid.webviewrequest = model.content_url;
    raid.labeltext = model.title;
    [self.navigationController pushViewController:raid animated:YES];
}

//添加假的返回按钮
-(void)getbutton
{
    self.backbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.backbutton.backgroundColor = [UIColor clearColor];
    self.backbutton.frame = CGRectMake(10, 32, 20, 20);
     [self.backbutton setBackgroundImage:[UIImage imageNamed:@"iconfont-xiangzuo@2x"] forState:UIControlStateNormal];
    [self.backbutton addTarget:self action:@selector(backaaa) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:self.backbutton];
    [self.view bringSubviewToFront:self.backbutton];
}
//返回上一页面的button
-(void)backaaa
{
    [self.navigationController popViewControllerAnimated:YES];
    //    NSLog(@"走走走 回家喽");
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
